<?php
//#(SELECT COUNT(tpc_id)FROM zk_innoforum_topics WHERE pn_uid = tpc_cr_uid ) AS 'count_topics',
function smarty_function_getuser_has_topic($params, &$smarty) {
    //Get value from page
    $page    = $params['page'];
    $pagelen = $params['pagelen'];
    $uname   = trim($params['arg_uname']);
    $type    = $params['arg_type'];
      
     if(!isset($page)){
       $page = 1;
     }

    if(isset($uname) && trim($uname) != "" && isset($type) && $type =="uname"){

      //Query total data
      $sql = "SELECT
                COUNT(tpc_id) AS 'count_topics'
              FROM
                zk_innoforum_topics ,
                zk_users
              WHERE
                pn_uid = tpc_cr_uid
              AND
                pn_uname like '%".$uname."%'
              GROUP BY
                tpc_cr_uid
              ORDER BY
                count_topics DESC";

      $column = array("count_topics");
      $result = DBUtil::executeSQL($sql);
      $objectArray = DBUtil::marshallObjects ($result, $column);
      $num_row = sizeof($objectArray);
      $totalpage = ceil($num_row / $pagelen);
      $goto = ($page - 1) * $pagelen;

      //Query data
      $sql = "SELECT
                tpc_cr_uid,
                pn_uname,
                COUNT(tpc_id) 'count_topics' 
              FROM
                zk_innoforum_topics ,
                zk_users
              WHERE
                pn_uid = tpc_cr_uid
              AND
                pn_uname like '%".$uname."%'
              GROUP BY
                tpc_cr_uid
              ORDER BY
                count_topics DESC
              LIMIT ".$goto.",".$pagelen;

    }else if(isset($uname) && trim($uname) != "" && isset($type) && $type =="uda_uname"){

      //Query total data
      $sql = "SELECT
                COUNT(tpc_id) AS 'count_topics'
              FROM
                zk_innoforum_topics ,
                zk_users,
                zk_user_data
              WHERE
                pn_uid = tpc_cr_uid
              AND
                pn_uid = pn_uda_uid
              AND
                tpc_cr_uid = pn_uda_uid
              AND
                pn_uda_propid =1
              AND
                pn_uda_value like '%".$uname."%'
              GROUP BY
                tpc_cr_uid
              ORDER BY
                count_topics DESC";

      $column = array("count_topics");
      $result = DBUtil::executeSQL($sql);
      $objectArray = DBUtil::marshallObjects ($result, $column);
      $num_row = sizeof($objectArray);
      $totalpage = ceil($num_row / $pagelen);
      $goto = ($page - 1) * $pagelen;

      //Query data
      $sql = "SELECT
                tpc_cr_uid,
                pn_uname,
                COUNT(tpc_id) 'count_topics' 
              FROM
                zk_innoforum_topics ,
                zk_users,
                zk_user_data
              WHERE
                pn_uid = tpc_cr_uid
              AND
                pn_uid = pn_uda_uid
              AND
                tpc_cr_uid = pn_uda_uid
              AND
                pn_uda_propid =1
              AND
                pn_uda_value like '%".$uname."%'
              GROUP BY
                tpc_cr_uid
              ORDER BY
                count_topics DESC
              LIMIT ".$goto.",".$pagelen;
    }else {
      //Query total data
      $sql = "SELECT
                COUNT(tpc_id) AS 'count_topics'
              FROM
                zk_innoforum_topics ,
                zk_users
              WHERE
                pn_uid = tpc_cr_uid
              GROUP BY
                tpc_cr_uid
              ORDER BY
                count_topics DESC";

      $column = array("count_topics");
      $result = DBUtil::executeSQL($sql);
      $objectArray = DBUtil::marshallObjects ($result, $column);
      $num_row = sizeof($objectArray);
      $totalpage = ceil($num_row / $pagelen);
      $goto = ($page - 1) * $pagelen;

      //Query data
      $sql = "SELECT
                tpc_cr_uid,
                pn_uname,
                COUNT(tpc_id) 'count_topics' 
              FROM
                zk_innoforum_topics ,
                zk_users
              WHERE
                pn_uid = tpc_cr_uid
              GROUP BY
                tpc_cr_uid
              ORDER BY
                count_topics DESC
              LIMIT ".$goto.",".$pagelen;
    }

    $column = array("uid","uname","count_topics");
    $result = DBUtil::executeSQL($sql);
    $objectArray = DBUtil::marshallObjects ($result, $column);
    $smarty->assign("countArray", $objectArray);
    $smarty->assign("totalpage", $totalpage);
}

